
package com.j2eefast.produce.pms.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsSchemeEntity;
import com.j2eefast.produce.pms.entity.PmsTemplateEntity;
import com.j2eefast.produce.pms.mapper.PmsTemplateMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>模板管理</p>
 *
 * @author: summer
 * @date: 2021-05-29 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsTemplateService extends ServiceImpl<PmsTemplateMapper, PmsTemplateEntity> {

	@Resource
	protected PmsTemplateMapper pmsTemplateMapper;
	@Resource
	@Lazy
	private SerialService pmsSerialService;

	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
		String templateCode = (String) params.get("templateCode");
		String templateCode2 = (String) params.get("templateCode2");
		String templateName = (String) params.get("templateName");
		String templateName2 = (String) params.get("templateName2");
		String templateType = (String) params.get("templateType");
		String templateUrl = (String) params.get("templateUrl");
		String beginTime = (String) params.get("beginTime");
		String endTime = (String) params.get("endTime");


		Page<PmsTemplateEntity> page = pmsTemplateMapper.findPage(
				new Query<PmsSchemeEntity>(params).getPage(),
				StrUtil.nullToDefault(templateCode,""),
				StrUtil.nullToDefault(templateCode2,""),
				StrUtil.nullToDefault(templateName,""),
				StrUtil.nullToDefault(templateName2,""),
				StrUtil.nullToDefault(templateType,""),
				StrUtil.nullToDefault(templateUrl,""),
				StrUtil.nullToDefault(beginTime,""),
				StrUtil.nullToDefault(endTime,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return pmsTemplateMapper.deleteByIds(ids);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean updateCheck(Long id) {
		Long userId= UserUtils.getUserInfo().getId();

		return pmsTemplateMapper.updateCheck(id ,userId);

	}

	/**
	 * 添加
	 *
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean saves(PmsTemplateEntity taskFrom){
//		Long userId= UserUtils.getUserInfo().getId();
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		taskFrom.setUpdateTime(date);
		taskFrom.setCreateTime(date);
		String userName=UserUtils.getUserInfo().getName();
		taskFrom.setCreateBy(userName);
		taskFrom.setUpdateBy(userName);
		PmsTemplateEntity pmsTemplateEntity = pmsTemplateMapper.selectTemplate();
		Long total=0L;
		if(pmsTemplateEntity!=null && pmsTemplateEntity.getTotal()!=null){
			total=pmsTemplateEntity.getTotal();
		}
		Map<String ,String> participant=pmsSerialService.formatTitle(total,"默认序列号");//编号
		taskFrom.setTemplateCode(participant.get("title"));
		taskFrom.setTotal(Long.parseLong(participant.get("total")));
		return this.save(taskFrom);

	}



}
